#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/28 14:50
# @Author : 闫峰
# @File    : redis_utils.py
import redis

from conf.settings import ensure_path_sep
from utils.readFileUtils.yaml_control import GetYamlData


# from conf.settings import ensure_path_sep
# from utils import GetYamlData


class RedisClient:
    def __init__(self,reids_ksy):
        self.config = GetYamlData(ensure_path_sep("\\conf\\redis.yaml")).get_yaml_data()[reids_ksy]
        self.redis = redis.Redis(host=self.config['host'], port=self.config['port'], db=self.config['database'], password=self.config['password'])

    def set(self, key, value):
        self.redis.set(key, value)

    def get(self, key):
        return self.redis.get(key).decode('utf-8')

    def delete(self, key):
        self.redis.delete(key)

    def get_keys(self,a):
        key_names = self.redis.keys(a,encodings='utf-8')
        return [name.decode('utf-8') for name in key_names]


if __name__ == '__main__':
    database_key = 'java_redis'
    r = RedisClient(database_key)

    # setup_sql = "delete:AllegroStrategy:ExecTime:*"
    # funcname = setup_sql.split(":",maxsplit=1)[0]
    # key = setup_sql.split(":",maxsplit=1)[1]
    # func = getattr(r, funcname)
    # func(key)
    #
    # print(r.get_keys('walmart:feed:sku*'))
    # print(r.get("ATTRIBUTE:TRANSLATION:ages_18_and_over_x_rated"))
    print(r.get("org_captcha:891da0b8-b109-4108-8298-bb15234f5c08"))
